% Yosemite
% Angular error: Mean 7.857 STD: 7.923
% Absolute error: Mean 0.265 STD: 0.343

% Rubber Whale
% Angular error: Mean 10.476 STD: 15.220
% Absolute error: Mean 0.343 STD: 0.487

close all
clc
clear variables
dbstop if error

sizeRatio = 1;

% [ images flowXGT flowYGT masks ] = YosemiteDataset;
% ug = flowXGT{1};
% vg = flowYGT{1};


images{1} = imread('D:\Vic\Work\Src\OF\TestData\Middlebury\other-color-allframes\other-data\RubberWhale\frame10.png');
images{2} = imread('D:\Vic\Work\Src\OF\TestData\Middlebury\other-color-allframes\other-data\RubberWhale\frame11.png');
[ uvg ]  = readFlowFile('D:\Vic\Work\Src\OF\TestData\Middlebury\other-gt-flow\RubberWhale\flow10.flo');
uvg(uvg > 1000) = 0;
ug = uvg(:, :, 1);
vg = uvg(:, :, 2);


ug = imresize(ug, sizeRatio);
vg = imresize(vg, sizeRatio);


im1 = imresize(images{1}, sizeRatio);
im2 = imresize(images{2}, sizeRatio);

% 
% im1rgb(:, :, 1) = im1;
% im1rgb(:, :, 2) = im1;
% im1rgb(:, :, 3) = im1;
% 
% im2rgb(:, :, 1) = im2;
% im2rgb(:, :, 2) = im2;
% im2rgb(:, :, 3) = im2;

[ u v ] = HornSchunkRobust({ im1 im2 }, HornSchunkParams);
%[u, v] = optic_flow_brox(im1rgb, im2rgb);


figure;
subplot(1, 2, 1);
hold on; title('Estimated Flow');
imshow(MyFlowToColor(u, v ))
subplot(1, 2, 2);
hold on; title('Ground Truth Flow');
imshow(MyFlowToColor(ug, vg))

%angError  = AngularError(u .* masks{1}, v .* masks{1}, ug .* masks{1}, vg .* masks{1});
angError  = AngularError(u , v , ug , vg);
meanError = mean(angError(:));
stdError = std(angError(:));

%absError = AbsError(u .* masks{1}, v .* masks{1}, ug .* masks{1}, vg .* masks{1});
absError = AbsError(u , v , ug, vg );
meanAbsError = mean(absError(:));
StdAbsError = std(absError(:));

fprintf(1, 'Angular error: Mean %.3f STD: %.3f\n', meanError, stdError);
fprintf(1, 'Absolute error: Mean %.3f STD: %.3f\n', meanAbsError, StdAbsError);

%  uv = estimate_flow_interface(im1, im2, 'classic-c-a');
% error2  = AngularError(uv(:, :, 1), uv(:, :, 2), ug, vg);
% meanError2 = mean(error2(:))
% 
% figure, imshow(MyFlowToColor(uv(:, :, 1), uv(:, :, 2), 0.1))